package weka;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import fe.FeatureConfig;
import fe.FeatureManager;
import fe.SchemaManager;
import fe.FeatureConfig.FeatureType;

public class WekaDataExporter {
	public WekaDataExporter(String configPath) throws IOException{
		featureConfig = new FeatureConfig(configPath);
	}
	
	private SchemaManager baselineSchemaManager = new SchemaManager();
	private SchemaManager productionSchemaManager = new SchemaManager();
	private FeatureConfig featureConfig;
	
	public void exportSchema(String dataPath, String wekaPath, String positiveWeight) throws Exception{
		
		String baselinePath = wekaPath + "_baseline.arff";
		String productionPath = wekaPath + "_production.arff";
		System.out.println("start extract weka data ...");
		System.out.println("dataPath: " + dataPath);
		System.out.println("baselinePath: " + baselinePath);
		System.out.println("productionPath: " + productionPath);
		System.out.println("positiveWeight: " + positiveWeight);
		System.out.println("Continue(0/1)?");
		Scanner scanner = new Scanner(System.in);
		int c = scanner.nextInt(); 
		if (c == 0)
			return ;
		double weight = Double.parseDouble(positiveWeight);
		baselineSchemaManager.initManager(featureConfig, FeatureType.BASELINE);
		productionSchemaManager.initManager(featureConfig, FeatureType.PRODUCTION);
		
		File baselineFile = new File(baselinePath);
		File productionFile = new File(productionPath);
		
		//extract schema
		System.out.println("start extract weka data schema ...");
		File dataFile = new File(dataPath);
		BufferedReader reader = new BufferedReader(new FileReader(dataFile));
		String line;
		ArrayList<FeatureType> featureTypes = featureConfig.getFeatureTypes();
		int clkCount = 0;
		int nclkCount = 0;
		while ((line = reader.readLine()) != null) {
			String[] features = line.trim().split("\t", 2)[1].split(FeatureManager.FEATURE_SEPARATOR);
			if (features[featureTypes.size() - 1].equalsIgnoreCase("'CLK'"))
				++clkCount;
			else 
				++nclkCount;
			baselineSchemaManager.checkFeaturesStr(features);
			productionSchemaManager.checkFeaturesStr(features);
		}		
		reader.close();
		//write schema
		System.out.println("start write weka data schema ...");
		BufferedWriter baselineWriter = new BufferedWriter(new FileWriter(baselineFile));
		BufferedWriter productionWriter = new BufferedWriter(new FileWriter(productionFile));		
		baselineWriter.write(baselineSchemaManager.getFeatureSchema());
		productionWriter.write(productionSchemaManager.getFeatureSchema());
		//write data
		System.out.println("start extract weka data ...");
		int base;
		if (clkCount == 0 || weight == 1)
			base = 1;
		else
			base= (int)((nclkCount * weight) / (clkCount - clkCount * weight));
		String baseStr = ",{" + String.valueOf(base) + "}\n";
		reader = new BufferedReader(new FileReader(dataFile));
		while ((line = reader.readLine()) != null) {
			String[] features = line.trim().split("\t", 2)[1].split(FeatureManager.FEATURE_SEPARATOR);
			StringBuilder baselineData = new StringBuilder();
			StringBuilder productionData = new StringBuilder();
			for (int i = 0; i < features.length; ++i) {
				if (featureTypes.get(i) == FeatureType.BASELINE) {
					if (baselineData.length() > 0)
						baselineData.append(FeatureManager.FEATURE_SEPARATOR);
					baselineData.append(features[i]);
				}
				if (featureTypes.get(i) != FeatureType.USELESS) {
					if (productionData.length() > 0)
						productionData.append(FeatureManager.FEATURE_SEPARATOR);
					productionData.append(features[i]);
				}					
			}
			if (features[featureTypes.size() - 1].equalsIgnoreCase("'CLK'")) {
				baselineData.append(baseStr);			
				productionData.append(baseStr);			
			}
			else {
				baselineData.append("\n");			
				productionData.append("\n");
			}
			baselineWriter.write(baselineData.toString());
			productionWriter.write(productionData.toString());
		}
		
		baselineWriter.close();
		productionWriter.close();
		System.out.println("export successfully!");
		
	}
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if (args.length != 3) {
			System.out.println("Usage: <dataFile> <outputPathPrefix> <positiveWeight>");
			System.exit(1);
		}
		WekaDataExporter wekaExporter = new WekaDataExporter(FeatureConfig.DEFAULT_CONFIG_PATH);
		wekaExporter.exportSchema(args[0], args[1], args[2]);
		System.exit(0);
	}
}
